// pages/jiangpin/jiangpin.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    ggid: '',
    gglist: '',
    jplist: '',
    name: '',
    navlist0:[
      '不限','兑换数量排序','兑换钻石排序'
    ],
    type:0,
    type1:0,
    navlist1:[
      {
        "id": '', //ID
        "name": "不限" //奖品大类名称
        }
    ],
    navlist2: [{
      id: "1",
      title: "无运费",
      xz_flag: false
    },
    {
      id: "2",
      title: "可兑换",
      xz_flag: false
    }
    ],
    saixuan: false,
    page_no: 1,
    page_no1: 1
  },
  z_open() {
    this.setData({
      saixuan: true
    })
  },
  z_close(){
    let navlist2 = [{
      id: "1",
      title: "无运费",
      xz_flag: false
    },
    {
      id: "2",
      title: "可兑换",
      xz_flag: false
    }
    ]
    this.setData({
      type:0,
      type1:0,
      navlist2:navlist2
    })
  },
  z_wancheng(){
    this.setData({
      saixuan:false
    })
    this.z_jiangpin()
  },
  z_sea(e) {
    this.setData({
      name: e.detail.value
    })
  },
  z_jiangpin1(){
    this.z_jiangpin()
  },
  z_shaixuan1(e){
    this.setData({
      type:e.currentTarget.dataset.index
    })
    this.z_jiangpin()
  },
  z_shaixuan2(e){
    this.setData({
      type1:e.currentTarget.dataset.index
    })
  },
  z_shaixuan3: function (e) {
    let _this = this
    let index = e.currentTarget.dataset.index
    let xz_flag = e.currentTarget.dataset.xz_flag
    let id = e.currentTarget.dataset.id
    if (xz_flag) {
      _this.setData({
        [`navlist2[${index}].xz_flag`]: false,
      })
      let arr = _this.data.vip_xz.indexOf(id)
      let vip_xz = _this.data.vip_xz
      vip_xz.splice(arr, 1)
      _this.setData({
        vip_xz: vip_xz
      })
    } else {
      _this.setData({
        [`navlist2[${index}].xz_flag`]: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.guanggao1()
    this.z_jiangpin()
    // this.z_jilu2()
    this.z_yonghu()
    this.z_idxgg()
    this.z_jilu3()
    this.z_jilu4()
  },
  z_jilu3(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/config/unprizeList?fbwz_type=2',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          mylist:res.data.data
        })
      }
    })
   },
   z_jilu4(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/config/dhjpdlList',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        let navlist1 = [
          {
            "id": '', //ID
            "name": "不限" //奖品大类名称
            }
        ]
        for(let i=0;i<res.data.data.length;i++){
          navlist1.push(res.data.data[i])
        }
        _this.setData({
          navlist1:navlist1
        })
      }
    })
   },
  guanggao1() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advertPage',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        console.log(res.data.data)
        _this.setData({
          ggid: res.data.data[0].id
        })
        _this.guanggao2()
      }
    })
  },
  guanggao2() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=4',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          gglist: res.data.data[0]
        })
      }
    })
  },
  z_kvlianjia(e){
    let _this = this
    let link_url = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.url
    if(link_url != undefined){
      wx.navigateTo({
        url: link_url,
      })
    }
  },
  z_jiangpin() {
    let _this = this
    let con_type
    console.log(_this.data.type1)
    if(_this.data.type1 == 0){
      con_type = '0'
    } else if(_this.data.type1 == 1){
      con_type = '2'
    } else if(_this.data.type1 > 1){
      con_type = '1'
    }
    let jpdl
    if(_this.data.type1 > 1){
      jpdl = _this.data.navlist1[_this.data.type1].id
    } else {
      jpdl = '0'
    }
    let sx_wyf
    if(_this.data.navlist2[0].xz_flag == true){
      sx_wyf = '1'
    } else {
      sx_wyf = '0'
    }
    let sx_kdh
    if(_this.data.navlist2[1].xz_flag == true){
      sx_kdh = '1'
    } else {
      sx_kdh = '0'
    }
    let params = {
      "token": wx.getStorageSync("user_token"),
      "page_no": _this.data.page_no, //第几页
      "page_num": "500", //每页显示数量
      "type": "1", //奖品类型 0-不限 1-兑换奖品  2-推荐活动奖品
      "con_type": '0', //奖品内容  0-不限  1-实物 2-虚拟 3-金币 4-钻石 5-夺宝券
      "jpdl": jpdl, //奖品大类id
      "name": _this.data.name, //奖品名称
      "order_type": _this.data.type, //排序  0 不限制   1-兑换数量降序   2-兑换钻石升序
      "sx_wyf": sx_wyf, //筛选条件   1-无运费
      "sx_kdh": sx_kdh //筛选条件   1-可兑换
    }
    wx.request({
      url: app.apiurl + 'api/prize/list',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          jplist: res.data.data.list,
          saixuan: false
        })
      }
    })
  },
  z_jilu2() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/lotteryRecord?page_no=' + _this.data.page_no1 + '&page_num=10&type=2',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          list2: res.data.data.list
        })
      }
    })
  },
  z_yonghu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/myInfo',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.code == '1001') {
          _this.setData({
            login: true,
            yonghu: res.data.data
          })
        }
        if (res.data.data.yd_status == 3) {
          _this.z_yindao()
        }
      }
    })
  },
  z_yindao() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "yd_status": '4'
    }
    wx.request({
      url: app.apiurl + 'api/member/upMyYd',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {}
    })
  },
  z_idxgg() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/home/index',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          login: true,
          idx_gg: res.data.data,
          // time1:res.data.data.activity_info.diff_time
        })
        // _this.z_time()
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //下滑刷新
    //  let _this = this
    //  let page_no  = _this.data.page_no
    //  page_no = page_no+1
    //  let page_no1  = _this.data.page_no1
    //  page_no1 = page_no1+1
    //  _this.setData({
    //    page_no:page_no,
    //    page_no1:page_no1
    //  })
    //  _this.z_jiangpin()
    //  _this.z_jilu1()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})